<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>file/shell/smooth/resources/css/jquery.multiselect.css">
<script src="<?php echo base_url(); ?>file/shell/smooth/resources/scripts/jquery.multiselect.min.js"></script>

<div class="page-title">
  <h2>New Consignment</h2>
</div>

<?php if(validation_errors()) : ?>
  <div class="box"><div id="box-messages">
    <div class="messages">
      <div id="message-success" class="message message-error">
        <div class="image">
          <img src="<?php echo base_url(); ?>file/shell/smooth/resources/images/icons/error.png" alt="Error" height="32" />
        </div>
        <div class="text"><?php echo validation_errors(); ?></div>
      <div class="dismiss"><a href="#message-success"></a></div>
      </div>
    </div>
  </div></div>
<?php endif; ?>

<?php
  $msg = $this->session->flashdata('message');
  $div_class = $this->session->flashdata('div_class');

  if($msg){
  echo '<div class="box"><div id="box-messages">
      <div class="messages">
        <div id="message-success" class="message message-'.$div_class.'">
          <div class="image">
            <img src="'. base_url() .'file/shell/smooth/resources/images/icons/'.$div_class.'.png" alt="Success" height="32" />
          </div>
          <div class="text"><h6>'. $msg .'</h6></div>
          <div class="dismiss"><a href="#message-success"></a></div>
        </div>
      </div>
    </div></div>';
  }
?>

<div class="page page-trans">

<form action="<?php echo base_url();?>index.php/consignment/add" method="post">
  <table class="trans-info">
    <tr>
      <td>Territory</td>
      <td><strong><?php echo $region.', '.$cluster; ?></strong></td>
    </tr>
    <tr>
      <td>Tanggal Sell In</td>
      <td>
        <input type="text" name="trx_date" id="date" placeholder="yyyy-mm-dd" value="<?php echo date("Y-m-d")?>" readonly="readonly" required>
      </td>
    </tr>
    <tr>
      <td>Nama Sales</td>
      <td>
        <select name="sales_id" id="sales-id">
          <option value=""></option>
            <?php foreach ($sales_id as $key) : ?>
                <option value="<?php echo $key->user_group_id."_".$key->user_id; ?>"><?php echo $key->user_id.' | '.$key->user_group_caption.' | '.$key->user_name; ?></option>
            <?php endforeach; ?>
        </select>
      </td>
    </tr>
    <tr>
      <td>Pilih Partner</td>
      <td>
        <select name="partner_id" id="partner-id">
          <option value=""></option>
          <!-- Appened partner list goes here -->
        </select>
      </td>
    </tr>
    <tr>
      <td>Program Nasional</td>
      <td>
        <select name="program_nasional" id="program-nasional">
        </select>
      </td>
    </tr>
    <tr>
      <td>Program Cluster</td>
      <td>
        <select name="program_cluster" id="program-cluster">
        </select>
      </td>
    </tr>
    <tr>
      <td>Termin Pembayaran</td>
      <td>
        <select name="term_of_payment">
          <?php foreach ($termin as $term) {
              echo '<option value="'.$term->term_id.'" title="'.$term->term_desc.'">'.$term->term_name.'</option>';
          } ?>
        </select>
      </td>
    </tr>
  </table>

  <div>
    <table class="tabel_transaksi" width="100%">
      <thead>
        <tr>
          <th>#</th>
          <th> </th>
          <th>ICCID</th>
          <th>MDN</th>
          <th>Group Item</th>
          <th>Nama Item</th>
          <th>Harga</th>
        </tr>
      </thead>
      <tfoot>
        <tr>
          <td colspan="7">
            <textarea row="4" style="width: 99%" name="remark"></textarea>
          </td>
        </tr>
        <tr>
          <td colspan="7" class="align-center">
            <button class="button" id="button-simpan" role="button" >Simpan</button>
            <button class="button" id="button-reset" role="reset" >Batal</button>
          </td>
        </tr>
      </tfoot>
      <tbody class="daftar-item">
        <!-- di sini di append data agent/outlet -->
      </tbody>
    </table>
    
  </div>
</form>

</div>

<script type="text/javascript">
  $(document).ready(function() {
    // $('#button-simpan').click().attr('disabled', 'disabled');
    $("form").submit(function () { $('#button-simpan').attr('disabled', 'disabled'); });

    $('#sales-id').change(function(){
      var user = $(this).val().split('_');
      $('.daftar-item').empty();
      $('#partner-id').empty(); $('#partner-id').append(new Option("Pilih Partner",""));
      if(user) {
        $.post(
          // "<?php echo base_url();?>index.php/sell/get_partners/"+sales_id,
          "<?php echo base_url();?>index.php/sell/getOutletList/",
          {user_group_id:user[0], sales_id:user[1]},
          function(data) {
            if(data.length != 0) {
              $.each(data, function(key, val){
                $('#partner-id').append(new Option(val["channel_id"]+" | "+val["channel_name"], val["territory_id"]+"_"+val["channel_id"]));
              });
            }
            else 
              { alert("Tidak ada Journey Cycle"); }
          }, "json"
        );

        // Yang ini untuk menampilkan list item
        var iterasi = 1
        $.post(
          "<?php echo base_url(); ?>index.php/sell/getItemList",
          {sales_id:user[1]},
          function(data) {
            var _d = jQuery.parseJSON(data);
            $.each(_d, function(k, v) {
              var item = "<tr>"+
                         "<td>"+iterasi+"</td>"+
                         "<td><input type='checkbox' name='iccid[]' id='iccid_"+iterasi+"' value='"+v['iccid']+"' /></td>"+
                         "<td><label for='iccid_"+iterasi+"'>"+v['iccid']+"</label></td>"+
                         "<td>"+v['mdn']+"</td>"+
                         "<td>"+v['item_group_name']+"</td>"+
                         "<td>"+v['item_name']+"</td>"+
                         "<td>"+v['getprice']+"</td>"+
                         "</tr>";
              iterasi++;
              $('.daftar-item').append(item);
            });
          }
        );
      }

    });

    // --------------------------------------------------------------------------------------

    $('#partner-id').change(function() {
      var user = $('#sales-id').val().split('_');
      var partner = $(this).val().split('_');

      $('#program-nasional').empty(); $('#program-cluster').empty();
      $('#program-cluster').append(new Option('','') );
      $.post(
        "<?php echo base_url();?>index.php/sell/get_program/"+user[0]+"/"+partner[1],
        function(data) {
          $.each(data, function(key, val){
            var programs = "<option value='"+val["program_id"]+"'>"+val["caption"]+"</option>";
            if(val["territory_id"] == 1)
              { $('#program-nasional').append(programs); }
            else
              { $('#program-cluster').append(programs); }
          });
        }, "json"
      );
    });

    // --------------------------------------------------------------------------------------

    

  });
</script>